package pl.edu.agh.ki.ewywiadowka.client.windows.educator;

import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.ScrollPanel;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.StudentDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.SubjectDTO;
import pl.edu.agh.ki.ewywiadowka.client.windows.GenericWindow;

public class EducatorMarksWelcomeWindow extends GenericWindow implements ClickHandler {
	
	private ScrollPanel _mainPanel = new ScrollPanel();
	private DockPanel _dockPanel = new DockPanel();
	private HorizontalPanel _hPanel = new HorizontalPanel();
	private Grid _grid = new Grid(2, 3);
	
	public ListBox _classListBox = new ListBox();
	public ListBox _studentsListBox = new ListBox();
	public ListBox _subjectsListBox = new ListBox();
	
	public Button _okButton = new Button("Dalej");
	
	private int _mode = -1;
	
	private ClassDTO _class = null;
	private LinkedList<StudentDTO> _students = null;
	private LinkedList<SubjectDTO> _subjects = null;
	
	public EducatorMarksWelcomeWindow(EWywiadowka wywiadowka, ClassDTO clas, int mode) {
		super(wywiadowka);
		
		_class = clas;
		_mode = mode;
		
		//_classListBox.setVisibleItemCount(15);
		_studentsListBox.setVisibleItemCount(15);
		_subjectsListBox.setVisibleItemCount(15);
		
		//_classListBox.addClickHandler(this);
		_studentsListBox.addClickHandler(this);
		_subjectsListBox.addClickHandler(this);
				
		//_grid.setWidget(0, 0, new Label("Wybierz klasę"));
		_grid.setWidget(0, 1, new Label("Wybierz ucznia"));
		_grid.setWidget(0, 2, new Label("Wybierz przedmiot"));
		
		//_grid.setWidget(1, 0, _classListBox);
		_grid.setWidget(1, 1, _studentsListBox);
		_grid.setWidget(1, 2, _subjectsListBox);
		
		
		_okButton.addClickHandler(this);
		_okButton.setEnabled(false);
		
		_dockPanel.add(_grid, DockPanel.CENTER);
		_dockPanel.add(_okButton, DockPanel.SOUTH);
		
		_mainPanel.add(_dockPanel);
		this.initWidget(_mainPanel);
		
		Const.eWywiadowkaService.getClassSquad(clas, new AsyncCallback<LinkedList<StudentDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(LinkedList<StudentDTO> result) {
				_students = result;
				
				_studentsListBox.clear();
				
				for (StudentDTO student : _students)
					_studentsListBox.addItem(student.toString());
				
			}
			
		});
		
		Const.eWywiadowkaService.getSubjects(new AsyncCallback<LinkedList<SubjectDTO>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
				
			}

			@Override
			public void onSuccess(LinkedList<SubjectDTO> result) {
				_subjects = result;
				
				_subjectsListBox.clear();
				
				for (SubjectDTO subject : _subjects)
					_subjectsListBox.addItem(subject.toString());
			}
			
		});
	}

	@Override
	public void onClick(ClickEvent event) {
		
		if (_studentsListBox.getSelectedIndex() != -1 && _subjectsListBox.getSelectedIndex() != -1)
			_okButton.setEnabled(true);
		else
			_okButton.setEnabled(false);
		
		if (event.getSource() == _okButton) {
			
			StudentDTO student = _students.get(_studentsListBox.getSelectedIndex());
			SubjectDTO subject = _subjects.get(_subjectsListBox.getSelectedIndex());
			
			if (_mode == 1)
				this.getWywiadowka().putWindow(new SingleMarksWindow(getWywiadowka(), student, subject, 1));
			else
				this.getWywiadowka().putWindow(new FinalMarksWindow(getWywiadowka(), student, subject, 1));
			
		}
	}

}
